package com.bff.gaia.streaming.api.mo.gaia.io.basiccoder;

import java.io.*;
import java.util.Arrays;

public class StringDoubleCoder {
	private String cmd;
	private String string;
	private Double val;
	private byte[] data;

	public StringDoubleCoder(byte[] data) {
		this.data = data;
		this.makeValue();
	}

	public StringDoubleCoder(String string, Double val, String cmd) {
		this.string = string;
		this.val = val;
		this.cmd = cmd;
		this.makeBytes();
	}

	private void makeValue() {
		DataInputStream din = new DataInputStream(new ByteArrayInputStream(this.data));

		try {
			int cmdLen = din.readInt();
			byte[] cmdData = new byte[cmdLen];
			din.readFully(cmdData);
			StringCoder cmdCoder = new StringCoder(cmdData);
			this.cmd = cmdCoder.getString();

			int strLen = din.readInt();
			byte[] strData = new byte[strLen];
			din.readFully(strData);
			StringCoder strCoder = new StringCoder(strData);
			this.string = strCoder.getString();

			int doubleLen = din.readInt();
			byte[] doubleData = new byte[doubleLen];
			din.readFully(doubleData);
			DoubleCoder doublecoder = new DoubleCoder(doubleData);
			this.val = doublecoder.getDouble();
		} catch (IOException var11) {
			var11.printStackTrace();
		}

	}

	private void makeBytes() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);

		StringCoder cmdCoder = new StringCoder(this.cmd);
		StringCoder strCoder = new StringCoder(this.string);
		DoubleCoder dCoder = new DoubleCoder(this.val);

		byte[] cmdData = cmdCoder.getBytes();
		byte[] strData = strCoder.getBytes();
		byte[] doubleData = dCoder.getBytes();

		try {
			dos.writeInt(cmdData.length);
			dos.write(cmdData);
			dos.writeInt(strData.length);
			dos.write(strData);
			dos.writeInt(doubleData.length);
			dos.write(doubleData);
		} catch (IOException var10) {
			var10.printStackTrace();
		}

		this.data = bos.toByteArray();
	}

	public String getCmd() {
		return this.cmd;
	}
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}
	public String getString() {
		return this.string;
	}
	public Double getDouble() {
		return this.val;
	}
	public byte[] getBytes() {
		return this.data;
	}

	@Override
	public String toString() {
		return "StringDoubleCoder{" +
			"cmd='" + cmd + '\'' +
			", string='" + string + '\'' +
			", val=" + val +
			", data=" + Arrays.toString(data) +
			'}';
	}
}